<?php

use App\Models\SaleAccount;
use Encore\Admin\Auth\Database\Menu;

/** @noinspection PhpComposerExtensionStubsInspection */

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

function curlGet($url) {

    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 0);
    if (substr($url, 0, 5) == 'https') {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);  // 从证书中检查SSL加密算法是否存在
    }

    $tmpInfo = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    return $tmpInfo;

}


//查看sql语句和执行时间
function debug_sql()
{
    $formatDuration = function($seconds) {
        if ($seconds < 0.001) {
            return round($seconds * 1000000).'μs';
        } elseif ($seconds < 1) {
            return round($seconds * 1000, 2).'ms';
        }

        return round($seconds, 2).'s';
    };

    \DB::listen(function($query) use($formatDuration) {
        $sql = $query->sql;
        $bindings = $query->bindings;
        $time = $query->time;//单位：ms
        $sqlWithPlaceholders = str_replace(['%', '?'], ['\'%%\'', '\'%s\''], $sql);
        $sql = vsprintf($sqlWithPlaceholders, $bindings);
        $time = $formatDuration($time / 1000);// 转成 每秒
        \Log::debug( 'SQL语句执行：'.$sql.'------耗时：'.$time);
    });
}

// 当前页面URI, 删除前缀
function getCurrentUri()
{
    $pre = config('admin.route.prefix');
    $uri = request()->route()->uri();
    return  \Illuminate\Support\Str::replaceFirst($pre.'/', '', $uri);
}


if (!function_exists('admin_base_path')) {
    /**
     * Get admin url.
     *
     * @param string $path
     *
     * @return string
     */
    function admin_base_path($path = '')
    {
        $prefix = '/'.trim(config('admin.route.prefix'), '/');

        $prefix = ($prefix == '/') ? '' : $prefix;

        $path = trim($path, '/');

        if (is_null($path) || strlen($path) == 0) {
            return $prefix ?: '/';
        }

        return $prefix.'/'.$path;
    }

    // 2021-01-01   =>
    function endDateTime($date)
    {
        return $date.' 23:59:59';
    }
}
